﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SapB1WebPOS.BLL;
using System.Data;
using System.Web.Services;
using System.Web.Script.Services;
using SapB1WebPOS.DAL;
using SapB1WebPOS.Utils;
using Newtonsoft.Json;
using SapB1WebPOS.Model;

namespace SapB1WebPOS
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string passWord = "sapb1addon.com.@123456789()";
                string expiredDate = System.Configuration.ConfigurationManager.AppSettings["ExpiredDate"].ToString();
                expiredDate = StringCipher.Decrypt(expiredDate, passWord);
                DateTime exDate = Utils.Utils.ConvertToDate(expiredDate);

                if (DateTime.Now.Date > exDate)
                {
                    Response.Redirect("TrialLicense.htm");
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                Response.Redirect("TrialLicense.htm");
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string GetLogin(string userName, string passWord, string companyDB, string companyName)
        {
            try
            {
                if (System.Configuration.ConfigurationManager.AppSettings["SapUser"] == null ||
                    System.Configuration.ConfigurationManager.AppSettings["SapPassword"] == null)
                {
                    return "Please set SAP user and password.";
                }
                string sapUserName = System.Configuration.ConfigurationManager.AppSettings["SapUser"].ToString();
                string sapPassword = System.Configuration.ConfigurationManager.AppSettings["SapPassword"].ToString();

                if (sapUserName.Length == 0 || sapPassword.Length == 0)
                {
                    return "Please set SAP user and password.";
                }

                if (userName == sapUserName && passWord == sapPassword)
                {
                    LoginBLL pmt = new LoginBLL();
                    string resultMess = pmt.GetLogin(userName, passWord, companyDB);
                    if (resultMess == "SUCCESS")
                    {
                        HttpContext.Current.Session["LoginUserName"] = userName;
                        HttpContext.Current.Session["LoginPassword"] = passWord;
                        HttpContext.Current.Session["CompanyDB"] = companyDB;
                        HttpContext.Current.Session["CompanyName"] = companyName;
                        HttpContext.Current.Session["IsSAPUser"] = true;

                        OnlineUser user = new OnlineUser();
                        user.Code = userName;
                        user.Name = userName;
                        user.U_Password = passWord;
                        user.U_GRPO = "Y";
                        user.U_PurchaseOrder = "Y";
                        user.U_PurchaseRequest = "Y";
                        user.U_SalesOrder = "Y";
                        user.U_SalesQuotation = "Y";
                        user.U_WebPOS = "Y";

                        HttpContext.Current.Session["UserPermission"] = user;


                        // Get Price
                        BusinessPartnersBLL bus = new BusinessPartnersBLL();
                        ItemMasterDataBLL itm = new ItemMasterDataBLL();
                        SAPConnection sapCon = new SAPConnection();

                        DataSet dsBus = new DataSet();
                        DataSet dsItm = new DataSet();

                        dsBus = bus.GetTop1OCRD(companyDB);
                        dsItm = itm.GetTop1OITM(companyDB);

                        if (dsBus != null
                            && dsBus.Tables.Count > 0
                            && dsBus.Tables[0].Rows.Count > 0
                            && dsItm != null
                            && dsItm.Tables.Count > 0
                            && dsItm.Tables[0].Rows.Count > 0)
                        {
                            string cardCode = dsBus.Tables[0].Rows[0]["CardCode"].ToString();
                            string itemCode = dsItm.Tables[0].Rows[0]["ItemCode"].ToString();
                            double price = sapCon.GetSAPItemPrice(userName, passWord, companyDB, cardCode, itemCode, 1, DateTime.Now.Date);
                        }
                    }
                    return resultMess;
                }
                else
                {
                    PermissionBLL per = new PermissionBLL();
                    DataSet dsUserPermission = per.GetOnlineUserByUserName(companyDB, userName, passWord);

                    if (dsUserPermission != null && dsUserPermission.Tables.Count > 0 && dsUserPermission.Tables[0].Rows.Count > 0)
                    {
                        HttpContext.Current.Session["LoginUserName"] = userName;
                        HttpContext.Current.Session["LoginPassword"] = passWord;
                        HttpContext.Current.Session["CompanyDB"] = companyDB;
                        HttpContext.Current.Session["CompanyName"] = companyName;
                        HttpContext.Current.Session["IsSAPUser"] = false;

                        OnlineUser user = new OnlineUser();
                        user.Code = dsUserPermission.Tables[0].Rows[0]["Code"].ToString();
                        user.Name = dsUserPermission.Tables[0].Rows[0]["Name"].ToString();
                        user.U_Password = dsUserPermission.Tables[0].Rows[0]["U_Password"].ToString();
                        user.U_GRPO = dsUserPermission.Tables[0].Rows[0]["U_GRPO"].ToString();
                        user.U_PurchaseOrder = dsUserPermission.Tables[0].Rows[0]["U_PurchaseOrder"].ToString();
                        user.U_PurchaseRequest = dsUserPermission.Tables[0].Rows[0]["U_PurchaseRequest"].ToString();
                        user.U_SalesOrder = dsUserPermission.Tables[0].Rows[0]["U_SalesOrder"].ToString();
                        user.U_SalesQuotation = dsUserPermission.Tables[0].Rows[0]["U_SalesQuotation"].ToString();
                        user.U_WebPOS = dsUserPermission.Tables[0].Rows[0]["U_WebPOS"].ToString();

                        HttpContext.Current.Session["UserPermission"] = user;

                        // Get Price
                        BusinessPartnersBLL bus = new BusinessPartnersBLL();
                        ItemMasterDataBLL itm = new ItemMasterDataBLL();
                        SAPConnection sapCon = new SAPConnection();

                        DataSet dsBus = new DataSet();
                        DataSet dsItm = new DataSet();

                        dsBus = bus.GetTop1OCRD(companyDB);
                        dsItm = itm.GetTop1OITM(companyDB);

                        if (dsBus != null
                            && dsBus.Tables.Count > 0
                            && dsBus.Tables[0].Rows.Count > 0
                            && dsItm != null
                            && dsItm.Tables.Count > 0
                            && dsItm.Tables[0].Rows.Count > 0)
                        {
                            string cardCode = dsBus.Tables[0].Rows[0]["CardCode"].ToString();
                            string itemCode = dsItm.Tables[0].Rows[0]["ItemCode"].ToString();
                            double price = sapCon.GetSAPItemPrice(sapUserName, sapPassword, companyDB, cardCode, itemCode, 1, DateTime.Now.Date);
                        }

                        return "SUCCESS";
                    }
                    else
                    {
                        return "The user or password you entered in incorrect.";
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllCompany()
        {

            CommonBLL obj = new CommonBLL();
            DataSet dsData = obj.GetAllCompany("sbo-common");
            if (dsData != null && dsData.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsData.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void Logout()
        {
            HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();
        }
    }
}